package com.eastchina.fw.springframework.beans.factory.support;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.util.ClassUtils;

public class FIDSIIBeanNameGenerator implements BeanNameGenerator {
	
	private static final String IMPL_SUFFIX = "Impl";

	public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
		try {
			String propertyName = ClassUtils.getShortNameAsProperty(Class.forName(definition.getBeanClassName()));
			int pos = propertyName.indexOf(IMPL_SUFFIX);
			if (pos == -1) {
				return propertyName;
			}
			return propertyName.substring(0, pos);

		} catch (ClassNotFoundException e) {
			throw new IllegalStateException("Class " + definition.getBeanClassName() + " not found.");
		}
	}

}
